package com.qiantu.phone.widget;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.qiantu.api.db.LLControllerDBManager;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.activity.AddDevice1Activity;
import com.qiantu.phone.ui.activity.RoomDeviceAddActivity;
import com.qiantu.phone.ui.activity.SmartControlActivity;

public class DeviceEmptyView extends LinearLayout {

    /**
     * 0 默认 1常用 2通用
     */
    private int type;

    public DeviceEmptyView(Context context, int type, String roomSerialNo) {
        this(context, null, type,roomSerialNo);
    }

    public DeviceEmptyView(Context context, AttributeSet attrs, int type, String roomSerialNo) {
        this(context, attrs, 0, type,roomSerialNo);
    }

    public DeviceEmptyView(Context context, AttributeSet attrs, int defStyleAttr, int type, String roomSerialNo) {
        super(context, attrs, defStyleAttr);
        this.type = type;
        StaggeredGridLayoutManager.LayoutParams layoutParams = new StaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //因为RecycleView的marginTop = -4dp;
        layoutParams.topMargin = getResources().getDimensionPixelSize(R.dimen.dp_4);
        setOrientation(VERTICAL);
        setLayoutParams(layoutParams);
        //由于当前view会添加到RecyclerView里，让setOnItemLongClickListener无效
        setEnabled(false);
        initView(roomSerialNo);
    }

    protected void initView(String roomSerialNo) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.empty_device_layout, this, true);
        TextView tvRoomName = view.findViewById(R.id.tv_room_name);
        if(type == 1){
            tvRoomName.setText(R.string.no_common_device);
        }else{
            tvRoomName.setText(R.string.current_room_no_device);
        }

        View btnAddDevice = view.findViewById(R.id.btn_add_device);
        if(AppApplication.getInstance().getCurHouseInfo().getRoleType() == 2){
            btnAddDevice.setVisibility(GONE);
        }
        btnAddDevice.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(LLControllerDBManager.getInstance(getContext()).getHostControllers().size() != 0){
                    Intent intent = new Intent(getContext(), RoomDeviceAddActivity.class);
                    if(type == 1){
                        intent.putExtra("type",1);
                    }else{
                        intent.putExtra("type",2);
                    }
                    intent.putExtra("roomSerialNo",roomSerialNo);
                    getContext().startActivity(intent);
                }else{
                    Intent intent = new Intent(getContext(), AddDevice1Activity.class);
                    getContext().startActivity(intent);
                }
            }
        });
    }

    public void updateView(boolean isTop){
        if(isTop){
            StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) getLayoutParams();
            layoutParams.topMargin = getResources().getDimensionPixelSize(R.dimen.dp_4);
        }else{
            StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) getLayoutParams();
            layoutParams.topMargin = -getResources().getDimensionPixelSize(R.dimen.dp_70);
        }
    }
}
